import { getDictByPatchType } from '@/api/dataDict'
import { getSeriesDataById } from '@/api/shopDesign'
import { deepClone } from '@/utils'
import { getPruductAllData } from '@/api/shopDesign'

export default {
  props: {
    initPrograms: {
      type: Array,
      default: () => []
    }
  },
  watch: {
    initPrograms: {
      handler(v) {
        this.selectedProgram = deepClone(v)
      },
      immediate: true,
      deep: true
    }
  },
  data() {
    return {
      itemList: [],
      mustList: [], // 必上方案列表
      activeModelDetail: {},
      programsZoneOption: [], // 区域下的方案区域
      sampleRecommendLevelOptions: [], // 推荐层级
      multipleSelection: [],
      spaceName: '', // 空间名
      activeChecked: false,
      activeProgramIndex: 0,
      activeProgramId: '', // 当前方案id
      zoneLoading: false,
      adviceCityLevelOptions: [],
      secondSampleSpaceOptions: [],
      shuldPayAmountOptions: [],
      houseTypeOptions: [],
      typePositionOptions: [],
      styleFormOptions: [],
      marketPosition: []
    }
  },
  async created() {
    this.getProgramList()
    const { RECOMMEND_LEVEL, HOUSE_TYPE, HOUSE_TYPE_POSITION, STYLE_FORM, MARKET_POSITION, SAMPLE_FUNCTION_SPACE, CITY_LEVEL, SUIT_DESIGN_WAY, SAMPLE_RECOMMEND_LEVEL } =
     await getDictByPatchType(['RECOMMEND_LEVEL', 'HOUSE_TYPE', 'HOUSE_TYPE_POSITION', 'STYLE_FORM', 'MARKET_POSITION', 'SAMPLE_FUNCTION_SPACE', 'CITY_LEVEL', 'SUIT_DESIGN_WAY', 'SAMPLE_RECOMMEND_LEVEL'])
    this.adviceCityLevelOptions = CITY_LEVEL
    this.secondSampleSpaceOptions = SAMPLE_FUNCTION_SPACE
    this.recommendLevelOptions = RECOMMEND_LEVEL
    this.shuldPayAmountOptions = SUIT_DESIGN_WAY
    this.marketPosition = MARKET_POSITION
    this.styleFormOptions = STYLE_FORM
    this.houseTypeOptions = HOUSE_TYPE
    this.typePositionOptions = HOUSE_TYPE_POSITION
    this.sampleRecommendLevelOptions = SAMPLE_RECOMMEND_LEVEL
  },
  methods: {
    canSelect(row, index) {
      if (row.isMustUp === 1) return false
      else return true
    },
    changeDesignWay() {
      this.$emit('select', this.programsZoneOption)
    },
    stopDefault(e) { e.stopPropagation() },
    initCheckAll(data) {

    },
    getProgramList() {
      getSeriesDataById(this.areaZone, this.programId).then(res => {
        if (res.success === 1 && res.data.length > 0) {
          this.programsZoneOption = res.data
          this.activeProgramId = res.data[0].id || ''
          this.programsZoneOption.map(el => {
            this.$set(el, 'selectItem', [])
            this.$set(el, 'checked', false)
            if (el.areaCode === 'Q003') this.$set(el, 'currentApplyDesignWay', [])
            this.$set(el, 'actualReportArea', 0)
          })
          this.programsZoneOption.map((el, keyIndex) => {
            this.selectedProgram.map(row => {
              if (el.spaceCode === row.spaceCode) {
                this.$set(el, 'actualReportArea', row.actualReportArea)
                this.$set(el, 'actualAreaSum', row.actualAreaSum)
                this.$set(el, 'checked', true)
                if (el.areaCode === 'Q003') this.$set(el, 'currentApplyDesignWay', row.currentApplyDesignWay && row.currentApplyDesignWay.split(','))
                this.programsZoneOption[keyIndex].selectItem.push(row)
              }
            })
            if (el.areaCode === 'Q002') {
              this.programsZoneOption[keyIndex].selectItem.forEach(el => {
                this.$set(el, 'cabinetColorList', (el.seriesStandardColorDTOList && el.seriesStandardColorDTOList.filter(v => v.mainProductName === '柜体')) || [])
                this.$set(el, 'doorColorList', (el.seriesStandardColorDTOList && el.seriesStandardColorDTOList.filter(v => v.mainProductName === '门板')) || [])
                this.$set(el, 'actureApplySampleSpace', (el.actureApplySampleSpace && el.actureApplySampleSpace.split(',')) || [])
                this.$set(el, 'cabinetColorName', (el.cabinetColorName && el.cabinetColorName.split(',')) || [])
                this.$set(el, 'doorColorName', (el.doorColorName && el.doorColorName.split(',')) || [])
              })
            }
            if (el.areaCode === 'Q015') {
              this.programsZoneOption[keyIndex].selectItem.forEach(el => {
                this.$set(el, 'customizedSeriesList', (el.assortCustomizedSeriesName && el.assortCustomizedSeriesName.split(',')) || [])
                this.$set(el, 'cupboardSeriesList', (el.assortCupboardSeriesName && el.assortCupboardSeriesName.split(',')) || [])
                this.$set(el, 'chooseCustomizedSeriesName', (el.chooseCustomizedSeriesName && el.chooseCustomizedSeriesName.split(',')) || [])
                this.$set(el, 'chooseCupboardSeriesName', (el.chooseCupboardSeriesName && el.chooseCupboardSeriesName.split(',')) || [])
              })
            }
          })
          // if(this.areaZone)
          this.zoneLoading = false
        }
      }).catch(() => {
        this.zoneLoading = false
      })
    },
    // 设置表格多选行高亮
    tableRowClassName({ row, rowIndex }) {
      const filterArr = this.programsZoneOption[this.activeProgramIndex].selectItem.filter(item => item.id === row.id)
      return filterArr.length > 0 ? 'selected-highlight' : ''
    },
    handleSelectionChange(val) {
    },
    deletProgram() {
      if (!this.singleJudge(this.multipleSelection, '删除', false)) {
        return
      }
      const tempArr = this.multipleSelection.filter(el => el.isMustUp)
      if (tempArr.length > 0) {
        this.$confirm('所选项包括必上方案,删除将导致所有方案删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const flag = this.programsZoneOption[this.activeProgramIndex].selectItem.filter(item => {
            const index = this.multipleSelection.findIndex(i => i.id || i.configProgramSeriesId === item.id || item.configProgramSeriesId)
            return index === -1
          })
          this.programsZoneOption[this.activeProgramIndex].selectItem = flag
        })
      } else {
        const flag = this.programsZoneOption[this.activeProgramIndex].selectItem.filter(item => {
          const index = this.multipleSelection.findIndex(i => i.id === item.id)
          return index === -1
        })
        this.programsZoneOption[this.activeProgramIndex].selectItem = flag
      }
      this.$emit('select', this.programsZoneOption)
    },
    handleDelete(item, keyIndex) {
      // if (item.isMustUp) {
      //   this.$confirm('此方案为必上，删除将取消所有方案选择，确定？', '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning'
      //   }).then(() => {
      //     this.programsZoneOption[this.activeProgramIndex].selectItem = []
      //     this.$emit('select', this.programsZoneOption)
      //     return
      //   })
      // } else {
      this.programsZoneOption[this.activeProgramIndex].selectItem.splice(keyIndex, 1)
      this.changeReferenceArea && this.changeReferenceArea()
      if (this.changeAreaItemSum) this.changeAreaItemSum()
      else {
        this.$emit('select', this.programsZoneOption)
      }
      // }
      if (this.programsZoneOption[this.activeProgramIndex].selectItem.length === 0) {
        this.programsZoneOption[this.activeProgramIndex].checked = false
      }
    },
    changeCheck(data) {
      if (!data) {
        this.programsZoneOption[this.activeProgramIndex].actualReportArea = 0
        this.programsZoneOption[this.activeProgramIndex].selectItem = []
      } else {
        getPruductAllData(this.activeProgramId).then(res => {
          if (res.success === 1) {
            this.mustList = res.data.records.filter(el => el.isMustUp)
            const obj = {}
            let tempArr = [...this.programsZoneOption[this.activeProgramIndex].selectItem, ...this.mustList]
            // 去重
            tempArr = tempArr.reduce(function(item, next) {
              obj[next.id] ? '' : obj[next.id] = true && item.push(next)
              return item
            }, [])
            this.programsZoneOption[this.activeProgramIndex].selectItem = tempArr.map(v => {
              return { ...v,
                cabinetColorName: ((v.seriesStandardColorDTOList && v.seriesStandardColorDTOList.filter(v => v.mainProductName === '柜体' && v.isUp)) || []).map(v => v.cabinetColorName),
                doorColorName: ((v.seriesStandardColorDTOList && v.seriesStandardColorDTOList.filter(v => v.mainProductName === '门板' && v.isUp)) || []).map(v => v.doorColorName),
                cabinetColorList: (v.seriesStandardColorDTOList && v.seriesStandardColorDTOList.filter(v => v.mainProductName === '柜体')) || [],
                doorColorList: (v.seriesStandardColorDTOList && v.seriesStandardColorDTOList.filter(v => v.mainProductName === '门板')) || [] }
            })
            this.changeReferenceArea && this.changeReferenceArea()
          }
        })
      }
      this.$emit('select', this.programsZoneOption)
    },
    handleChangePro(item, index) {
      this.activeProgramIndex = index
      this.spaceName = item.spaceName
      this.activeChecked = item.checked || false
      this.activeProgramId = item.id || ''
    }
  }
}

